package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController("AdminDishController")
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishServer;

    /**
     * 新增菜品
     * @param dishDTO 新增-修改菜品传递的数据模型
     * @return code 1, msg null, data null
     */
    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "DishCache", key = "#dishDTO.categoryId")
    public Result save (@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishServer.saveDishWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param dishPageQueryDTO 菜品分页查询传输模型
     * @return code 1, msg null, data pageResult
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询：{}",dishPageQueryDTO);
        PageResult pageResult = dishServer.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 批量删除
     * @param ids 删除的id集合
     * @return code 1, msg null, data null
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    @CacheEvict(cacheNames = "DishCache", allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除:{}",ids);
        dishServer.delete(ids);
        return Result.success();
    }

    /**
     * 启用禁用菜品
     * @param status 状态
     * @param id 菜品id
     * @return code 1, msg null, data null
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用菜品")
    @CacheEvict(cacheNames = "DishCache", allEntries = true)
    public Result StartOrStop (@PathVariable("status") Integer status, Long id ){
        log.info("启用禁用菜品：id:{} status:{}",id,status);
        dishServer.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id 菜品id
     * @return ode 1, msg null, data dishVO
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable("id") Long id){
        log.info("根据id查询菜品：{}",id);
        DishVO dishVO = dishServer.getDishWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 根据id修改菜品信息和口味信息
     * @param dishDTO 新增-修改菜品传递的数据模型
     * @return code 1, msg null, data null
     */
    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "DishCache", allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}",dishDTO);
        dishServer.updateDishWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品集合
     * @param categoryId 分类id
     * @return code 1, msg null, data dishList
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result list(Long categoryId){
        log.info("根据分类id查询菜品：{}",categoryId);
        List<Dish> dishList = dishServer.list(categoryId);
        return Result.success(dishList);
    }
}
